package cn.itcast.dao.impl;

import cn.itcast.dao.UserDao;
import cn.itcast.daomain.comment;
import cn.itcast.daomain.commodity;
import cn.itcast.daomain.purchase;
import cn.itcast.daomain.user;
import cn.itcast.util.JDBCUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class UserDaoImpl implements UserDao {
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<commodity> selec() {

        String sql="select * from commodity";
        return template.query(sql,new BeanPropertyRowMapper<commodity>(commodity.class));
    }

    @Override
    public user addUser(String username) {
        user user=null;

        try{
            String sql="select * from user where username = ?";
            user= template.queryForObject(sql,new BeanPropertyRowMapper<user>(user.class),username);
        }catch (Exception e){

        }
        return user;
    }

    @Override
    public void save(user user) {
        String sql="insert into user(id,username,account,password,user_img,user_pur,comm_user,status,code) values(?,?,?,?,?,?,?,?,?)";
        template.update(sql,user.getId(),user.getUsername(),user.getAccount(),user.getPassword(),user.getUser_img(),user.getUser_pur(),user.getComm_user(),user.getStatus(),user.getCode());
    }

    @Override
    public user findByCode(String code) {

            String sql="select * from user where code = ?";
            return template.queryForObject(sql,new BeanPropertyRowMapper<user>(user.class),code);
    }

    @Override
    public void updateStatus(user user) {
        String  sql="update user set status = 'Y' where id=?";
        template.update(sql,user.getId());
    }

    @Override
    public user login(String username, String password) {
        user ava = null;
        try{
            String sql="select * from user where username = ? and password = ? or phone = ? and password =?";
            ava= template.queryForObject(sql,new BeanPropertyRowMapper<user>(user.class),username,password,username,password);
        }catch (Exception e){
            
        }
        return ava;
    }

    @Override
    public commodity details(String id) {

        String sql="select * from commodity where shopId = ?";
        commodity comm=template.queryForObject(sql,new BeanPropertyRowMapper<commodity>(commodity.class),id);
        return comm;
    }

    @Override
    public int addSp(String foodList, String user, String len, String shou) {
        String sql="insert into purchase(id,user_pur,current,current_dat,number,name,home,pur_price,specs,Address,comment,comment_id) values(null,?,?,?,?,?,?,?,?,?,?,?)";
        JSONArray objects = JSON.parseArray(foodList);	 //解析对象数组
        int arr= Integer.parseInt(len);

        String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(Calendar.getInstance().getTime());

        Calendar timeStamp2 = Calendar.getInstance();
        timeStamp2.setTime(new Date());
        timeStamp2.add(timeStamp2.DAY_OF_MONTH,+3);
        String end=new SimpleDateFormat("yyyy-MM-dd HH:mm").format(timeStamp2.getTime());

        int arrr=0;
        for (int i = 0; i <arr ; i++) {
            JSONObject jsonObject = JSON.parseObject(objects.getString(i));
            arrr+=template.update(sql,user,timeStamp,end,jsonObject.get("quantity"),jsonObject.get("title"),jsonObject.get("picture"),jsonObject.get("total"),jsonObject.get("guige"),shou,"N",jsonObject.get("commentid"));
        }


        return arrr;
    }

    @Override
    public List<commodity> spList(String spName) {
        String sql="SELECT * FROM commodity WHERE name LIKE  CONCAT('%',?,'%')";

        return template.query(sql,new BeanPropertyRowMapper<commodity>(commodity.class),spName);
    }

    @Override
    public int updateUser(String username, String password, String pptPic) {
        String sql="update user set password = ? , user_img =? where  username = ? ";
        return template.update(sql,password,pptPic,username);
    }

    @Override
    public user chaxunl(String username) {
        String sql="select * from user where username = ?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<user>(user.class),username);
    }

    @Override
    public List<purchase> selecPur(String userid) {
        String sql="SELECT * FROM purchase WHERE user_pur= ?";
        return template.query(sql,new BeanPropertyRowMapper<purchase>(purchase.class),userid);
    }

    @Override
    public List<comment> comment_id(String commentId) {

        String sql="SELECT * FROM comment WHERE comment_id= ?";
        return template.query(sql,new BeanPropertyRowMapper<comment>(comment.class),commentId);
    }

    @Override
    public int updateuserPhoen(String id, String phone) {
        String sql="UPDATE user SET phone = ? WHERE id=?";
        return template.update(sql,phone,id);
    }

    @Override
    public user userObject(String userid) {
        String sql="select * from user where id= ?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<user>(user.class),userid);
    }

    @Override
    public int updatecomment(String comment, String commUser, String tup, String text, String userImg,String xin) {
        String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(Calendar.getInstance().getTime());
        String sql="insert into comment(id,comment_id,comm_user,comm_img,comm_text,comm_userImg,comm_date,comm_miqi) values(null,?,?,?,?,?,?,?)";

        return template.update(sql,comment,commUser,tup,text,userImg,timeStamp,xin);
    }

    @Override
    public int updatepur(String purid) {
        String sql="UPDATE purchase SET comment = ? WHERE id= ?";
        return template.update(sql,"Y",purid);
    }

    @Override
    public int updateUser(String username, String pptPic) {
        String sql="update user set user_img =? where  username = ? ";
        return template.update(sql,pptPic,username);
    }


}
